package com.rdgzs.framework.utilsTools;


import java.util.Date;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.rdgzs.framework.utilsTools.ConfigPropertiesUtils;

public class SimpleMailServiceHelper {
	//默认发送者
	public static void sendNotificationMailDefaultFromUser(String message,String toUserName){
		String fromUserName = ConfigPropertiesUtils.getValueByKey("mail.DefaultFromUser");
		ServletContext servletContext = ServletActionContext.getServletContext();
		ApplicationContext   ac   = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		//获取ApplicationContext.xml文件中定义的BEAN
		SimpleMailService  mail=(SimpleMailService)ac.getBean("simpleMailService");
		mail.sendNotificationMail(message,toUserName,fromUserName);
	}

	public static void sendNotificationMail(String message,String toUserName,String fromUserName){
		ServletContext servletContext = ServletActionContext.getServletContext();
		ApplicationContext   ac   = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		//获取ApplicationContext.xml文件中定义的BEAN
		SimpleMailService  mail=(SimpleMailService)ac.getBean("simpleMailService");
		mail.sendNotificationMail(message,toUserName,fromUserName);
	}

	//所有参数都是指定的
	public static void sendNotificationMail(String mailTitle,String mailcontent,String toUserName,String fromUserName,String textTemplate,Object ... textTemplateargs) {

		String mailFlag = ConfigPropertiesUtils.getValueByKey("mail.flag");
		if(mailFlag.equals("1")){//发mail
			ServletContext servletContext = ServletActionContext.getServletContext();
			ApplicationContext   ac   = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			//获取ApplicationContext.xml文件中定义的BEAN
			SimpleMailService  mail=(SimpleMailService)ac.getBean("simpleMailService");
			mail.setTextTemplate(textTemplate);
			mail.sendNotificationMail(mailTitle,mailcontent,toUserName,fromUserName,textTemplateargs);
		}

	}

	//使用默认Template,而其他参数都是指定的
	public static void sendNotificationMailDefaultTemplate(String mailTitle,String mailcontent,String toUserName,String fromUserName,Object ... textTemplateargs) {

		String mailFlag = ConfigPropertiesUtils.getValueByKey("mail.flag");
		if(mailFlag.equals("1")){//发mail
			ServletContext servletContext = ServletActionContext.getServletContext();
			ApplicationContext   ac   = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			//获取ApplicationContext.xml文件中定义的BEAN
			SimpleMailService  mail=(SimpleMailService)ac.getBean("simpleMailService");
			mail.sendNotificationMail(mailTitle,mailcontent,toUserName,fromUserName,textTemplateargs);
		}

	}


}
